package com.example.fms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.req.ActivityReq;
import com.example.fms.dto.resp.ActivityResp;
import com.example.fms.dto.resp.GoodsRespDto;
import com.example.fms.entity.Activity;
import com.example.fms.entity.ActivityUser;
import com.example.fms.entity.ArithmeticLogInfo;
import com.example.fms.exception.BusinessException;
import com.example.fms.exception.ExceptionEnum;
import com.example.fms.mapper.ActivityMapper;
import com.example.fms.mapper.ActivityUserMapper;
import com.example.fms.mapper.ArithmeticLogInfoMapper;
import com.example.fms.mapper.CategoriesAttributesMapper;
import com.example.fms.service.ActivityService;
import com.example.fms.service.ArithmeticLogInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Resource
    ActivityMapper activityMapper;
    @Resource
    ActivityService activityService;

    ActivityUserMapper activityUserMapper;

    @Override
    public Page<ActivityResp> getActivityList(ActivityReq req) {
        Page<ActivityResp> page = new Page<>(req.getPageNum(),req.getPageSize());
        List<ActivityResp> goodsList = activityMapper.getActivityList(page, req);
        page.setRecords(goodsList);
        return page;
    }

    @Override
    public Boolean release(ActivityReq req) {
       return null;
    }

    @Override
    public Boolean insert(ActivityReq req) {
        Activity activity = new Activity();
        if (req.getId() == null) {
            activity.setActivityName(req.getActivityName());
            activity.setState("进行中");
            activity.setLocation(req.getLocation());
            activity.setNumber(0L);
            activity.setMaxNumber(req.getMaxNumber());
            activity.setStartTime(req.getStartTime());
            activityService.save(activity);
        } else {
            activity = activityMapper.selectById(req.getId());
            activity.setActivityName(req.getActivityName());
            activity.setLocation(req.getLocation());
            activity.setMaxNumber(req.getMaxNumber());
            activity.setStartTime(req.getStartTime());
            activityService.updateById(activity);

        }

        return true;
    }

    @Override
    public Boolean delete(Long id) {
        activityMapper.deleteById(id);
        LambdaQueryWrapper<ActivityUser> qw = new LambdaQueryWrapper<>();
        qw.eq(ActivityUser::getActivityId,id);
        activityUserMapper.delete(qw);
        return  true;
    }

    @Override
    public Boolean updateActivity(ActivityReq req) {
        Activity activity = activityMapper.selectById(req.getId());
        if(StrUtil.isEmpty(req.getActivityName())) {
            activity.setActivityName(req.getActivityName());
        }
        if(StrUtil.isEmpty(req.getLocation())) {
            activity.setLocation(req.getLocation());
        }
        if(req.getMaxNumber() != null) {
            activity.setMaxNumber(req.getMaxNumber());
        }
        if(req.getStartTime() != null) {
            activity.setStartTime(req.getStartTime());
        }

        activityMapper.updateById(activity);
        return true;
    }

    @Override
    public ActivityResp getActivityById(Long id) {
        ActivityResp activityResp = new ActivityResp();
        Activity activity = activityMapper.selectById(id);
        if(activity != null) {
            activityResp.setId(activity.getId());
            activityResp.setActivityName(activity.getActivityName());
            activityResp.setState(activity.getState());
            activityResp.setLocation(activity.getLocation());
            activityResp.setNumber(activity.getNumber());
            activityResp.setMaxNumber(activity.getMaxNumber());
            activityResp.setStartTime(activity.getStartTime());
        }
        return activityResp;
    }

    @Override
    public void upDateActivityState() {
        LambdaQueryWrapper<Activity> qw = new LambdaQueryWrapper<>();
        qw.eq(Activity::getState, "进行中");
        List<Activity> list = activityMapper.selectList(qw);
        if (!list.isEmpty()) {
            list.forEach(s -> {
                if (s.getStartTime().getTime() < System.currentTimeMillis()) {
                    s.setState("已过期");
                    activityMapper.updateById(s);
                }
            });
        }
    }
}
